﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device;
using System.Device.Location;
using MoviTiendas.Logica;

namespace MoviTiendas.Sensores
{
    public class GPS
    {

        private Comunicacion.Comunicacion com;
        private Barrio barrio;
        private double kilom;
        GeoCoordinateWatcher watcher;
        private bool interna;

        public GPS(Comunicacion.Comunicacion comu, Barrio barrio, double kilom, bool interna )
        {
            com = comu;
            this.barrio = barrio;
            this.kilom = kilom;
            this.interna = interna;
           

            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default)
            {
                MovementThreshold = 20
            };

            watcher.PositionChanged += this.watcher_PositionChanged;
            watcher.StatusChanged += this.watcher_StatusChanged;
            watcher.Start();

        }

        private void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    // location is unsupported on this device
                    break;
                case GeoPositionStatus.NoData:
                    // data unavailable
                    break;
            }
        }

        private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            var epl = e.Position.Location;

            // Access the position information thusly:
            epl.Latitude.ToString("0.000");
            epl.Longitude.ToString("0.000");
            epl.Altitude.ToString();
            epl.HorizontalAccuracy.ToString();
            epl.VerticalAccuracy.ToString();
            epl.Course.ToString();
            epl.Speed.ToString();
            e.Position.Timestamp.LocalDateTime.ToString();
            watcher.Stop();
            if(!interna)
                com.buscarTiendasCercanas(barrio,kilom,epl.Latitude,epl.Longitude);
            else
                com.buscarInternaTiendasCercanas(barrio, kilom, epl.Latitude, epl.Longitude);
            
        }
    }
}
